<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02_双向链表</title>
</head>
<body>
    <script>
        //封装双向链表
        function DoubleLinkedList(){
            //默认属性
            this.head=null;
            this.tail=null;
            this.length=0;
            //内部节点类
            function Node(data){
                this.data=data;
                this.prev=null;
                this.next=null;
            }

            //追加方法
            DoubleLinkedList.prototype.append=function(data){
                //创建新节点
                var newNode=new Node(data);
                if(this.length===0){//是第一个节点
                    var newNode=new Node(data);
                    this.head=newNode;
                }else{//不是第一个节点
                    //找到最后一个节点
                    var current=this.head;
                    while (current.next) {
                        current=current.next;
                    }
                    //最后节点的next指向新的节点
                    current.next=newNode;
                }
                //长度+1
                this.length+=1;
            }

            DoubleLinkedList.prototype.toString = function(){
                return this.backwardString();
            }

            DoubleLinkedList.prototype.forwardString = function(){
                var current=this.tail;
                var res='';
                while (current) {
                    debugger
                    console.log(current)
                    res+=current.data+' ';
                    current=current.prev;
                }
                return res;
            }

            DoubleLinkedList.prototype.backwardString = function(){
                var current=this.head;
                var res='';
                while (current) {
                    res+=current.data+' ';
                    current=current.next;
                }
                return res;
            }

            DoubleLinkedList.prototype.insert = function(position,data){
                if(position< 0 || position > this.lenght) return false;
            }

        }
        var list=new DoubleLinkedList();
        list.append('asd')
        list.append('121')
        list.append('666')
        console.log('backwardString',list.backwardString())
        console.log('forwardString',list.forwardString())
    </script>
</body>
</html>